<?php


namespace app\api\controller\xilujob;


use app\admin\model\xilujob\JobCategory;
use app\admin\model\xilujob\SinglePage;
use app\common\controller\xilujob\XilujobApi;
use app\common\model\xilujob\Area;
use app\common\model\xilujob\Benefit;
use app\common\model\xilujob\CommentTag;
use app\common\model\xilujob\CompanyProperties;
use app\common\model\xilujob\CompanyScale;
use app\common\model\xilujob\Config;
use app\common\model\xilujob\EducationLevel;
use app\common\model\xilujob\IndustryCategory;
use app\common\model\xilujob\InformType;
use app\common\model\xilujob\NewsCategory;
use app\common\model\xilujob\ResumePersonalTag;
use app\common\model\xilujob\Salary;
use app\common\model\xilujob\SkillLevel;
use app\common\model\xilujob\WorkExperience;
use app\common\model\xilujob\WorkState;
use OSS\OssClient;

/**
 * 公共方法
 */
class Common extends XilujobApi
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $restrictedActions = ['*'];

    /**
     * @ApiTitle (职位举报类型)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.common/job_inform_types)
     */
    public function job_inform_types() {
        $lists = InformType::getAll();
        $this->success('查询成功', $lists);
    }
    /**
     * 获取省市
     */
    public function province_city(){
        $lists = \app\admin\model\xilujob\Area::getProvinceAndCity();
        $this->success('查询成功',$lists);
    }
    /**
     * 获得某城市的所有区
     */
    public function districts() {
        $cityId = $this->request->param('city_id');
        $districts = Area::where('level',3)->where('pid', $cityId)
            ->field('id,name')
            ->select();
        $this->success('查询成功',$districts);
    }

    /**
     * 获取省市区
     */
    public function area_tree(){
        $this->success('查询成功',Area::getAreaTree());
    }

    //行业
    public function industry_categories() {
        $categories = IndustryCategory::getAll();
        $this->success('查询成功', $categories);
    }

    public function company_properties() {
        $lists = CompanyProperties::getAll();
        $this->success('查询成功', $lists);
    }

    //规模
    public function company_scales() {
        $types = CompanyScale::getAll();
        $this->success('查询成功', $types);
    }


    /**
     * @ApiTitle (嵌套获取多层职位行业)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.common/job_categories)
     */
    public function job_categories() {
        $tree = JobCategory::getCategoryTree();
        $lists = $tree->getTreeArray(0);
        $this->success('查询成功', $lists);
    }


    /**
     * 返回区， 及旗下所有镇的数据
     */
    public function districts_with() {
        $cityId = $this->request->param('city_id');
        if(!$cityId) {
            $this->error();
        }
        $data = Area::getDistrictsWith($cityId);
        $this->success('查询成功',$data);
    }

    /**
     * @ApiTitle (获取月，天，时薪水)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.common/salary)
     * $ApiParams (name="type",type="int","description":"类型:1=时薪,2=日薪,3=月薪")
     */
    public function salary() {
        $type = $this->request->param('type');
        if($type==1) {
            $lists = Salary::getHourOptions();
        }else if($type==2) {
            $lists = Salary::getDailyOptions();
        }else if($type==3) {
            $lists = Salary::getMonthOptions();

        }else{
            $this->error(__('invalid arguments'));
        }
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (工作经验)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.common/work_experience)
     */
    public function work_experience() {
        $lists = WorkExperience::getAll();
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (学历)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.common/education_levels)
     */
    public function education_levels() {
        $lists = EducationLevel::getAll();
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (可供选择的福利)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.common/benefits)
     */
    public function benefits() {
        $lists = Benefit::getBenefits();
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (求职状态)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xilujob.common/work_states)
     */
    public function work_states() {
        $lists = WorkState::getAll();
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (技能熟练度)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xilujob.common/skill_levels)
     */
    public function skill_levels() {
        $lists = SkillLevel::getAll();
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (标签)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xilujob.common/resume_personal_tags)
     */
    public function resume_personal_tags() {
        $lists = ResumePersonalTag::getAll();
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (评论标签)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xilujob.common/comment_tags)
     */
    public function comment_tags() {
        $lists = CommentTag::getAll();
        $this->success('查询成功', $lists);
    }


    /**
     * @ApiTitle (新闻分类)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xilujob.common/news_category)
     */
    public function news_category() {
        $lists = NewsCategory::getAll();
        $this->success('查询成功', $lists);
    }

    /**
     * 图片上传
     */
    public function params()
    {
        $name = $this->request->post('name');
        $md5 = $this->request->post('md5');
        $auth = new \addons\alioss\library\Auth();
        $params = $auth->params($name, $md5);
        config('default_return_type','json');//返回json而不是html
        $this->success('', $params);
    }

    public function single_page() {
        $tag = $this->request->param('tag');
        $page = SinglePage::where('short_tag', $tag)->find();
        if($page) {
            $this->success('',['page'=>$page]);
        }
        $this->error('没有相关内容');
    }

    /**
     * 获取客服电话
     */
    public function getphone(){
        $this->success('',['phone'=>config("site.site_contact_phone")]);
    }

    /**
     * 获取对公账号信息
     */
    public function corporate(){
        $config['corporate_company'] = Config::getdatas('corporate_company');
        $config['open_bank'] = Config::getdatas('open_bank');
        $config['corporate_account'] = Config::getdatas('corporate_account');

        $this->success('',$config);
    }


    /**小程序二维码图片上传
     * @param $filePath
     * @param $fileData
     * @return bool
     */
    public static function uploadAlioss($filePath, $fileData) {
        $config = get_addon_config('alioss');
        if(empty($config) || !isset($config['accessKeyId']))  return true;
        $accessKeyId = $config['accessKeyId'];
        $accessKeySecret = $config['accessKeySecret'];
        $endpoint = $config['endpoint'];
        $bucket = $config['bucket'];
        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        $ossClient->putObject($bucket, ltrim($filePath, '/'),$fileData);
        return true;
    }

}